

'use strict';

//根节点的Controller，主要放一些全局共用的内容。
angular.module('mean.employ', ['ionic', 'ionic-datepicker']);
angular.module('mean.employ')
    .controller('UserMainController', function ($scope, $state, $http, $ionicHistory, $ionicLoading) {
        $scope.log = function(log) {
            $http.post('/wx/employ/log', {log: log});
        };
        $scope.goHome = function() {
            $state.go('user.index');
        };
        $scope.goBack = function() {
            if(!$ionicHistory.backView()) {
                $scope.goHome();
            } else {
                $state.go($ionicHistory.backView().stateName, $ionicHistory.backView().stateParams);
            }
        };
        $scope.tmp = {};
        $scope.cleanTmpData = function() {
            $scope.tmp = {};
        };
        $scope.refreshPage = {
            // to     去哪个页面
            // params 额外参数
        };
        $scope.forceRefresh = function(to, params) {
            $scope.refreshPage.to = to;
            $scope.refreshPage.params = params;
            $state.go('user.refreshPage');
        };
        $scope.publicData = {
            //userInfo    用户信息
            //userResumes 用户简历
            //favorites   收藏职位
            //history     浏览记录
            //isLoading   是否正在加载
            //popup       弹出框
        };
      
        $scope.publicData.isLoading = true;
        $scope.failureData = {
            // title
            // subTitle
        };

        $scope.$watch('publicData.isLoading', function() {
            if($scope.publicData.isLoading) {
                $ionicLoading.show({
                template: '<ion-spinner icon="android"></ion-spinner>',
                delay   : '250'
                });
            } else {
                $ionicLoading.hide();
            }
        });

        $scope.refreshData = function() {
            $scope.publicData.isLoading = true;
            return $http.get('/wx/employ/userInfo').then(function(success) {
                $scope.publicData.userInfo = success.data;
                return $http.get('/wx/employ/userResumes', {
                    params: {
                        userId: $scope.publicData.userInfo._id
                    }
                });
            }).then(function(success) {
                $scope.publicData.userResumes = success.data;
                return $http.get('/wx/employ/favorites', {
                    params: {
                        userId: $scope.publicData.userInfo._id
                    }
                });
            }).then(function(success) {
                $scope.publicData.favorites = success.data;
                return $http.get('/wx/employ/history', {
                    params: {
                        userId: $scope.publicData.userInfo._id
                    }
                });
            }).then(function(success) {
                $scope.publicData.history = success.data;
                $scope.publicData.isLoading = false;
            }).catch(function(err) {
                $state.go('user.failure');
                $scope.failureData.title = '用户数据加载失败';
                $scope.failureData.subTitle = '请稍后点击返回按钮重试';
                $scope.publicData.isLoading = false;
            });
        };
        $scope.refreshData(); 

        $scope.$on('$stateChangeStart', function(event){
            $scope.publicData.popup && $scope.publicData.popup.close && $scope.publicData.popup.close();
        });

    })
    .controller('FailureController', function($scope) {

    })
    .controller('RefreshPageController', function($scope, $state, $ionicHistory) {
        $ionicHistory.clearHistory();
        $state.go($scope.refreshPage.to, $scope.refreshPage.params);
    })
    .controller('HomeIndexController', function($scope,$http, $state, $ionicPopover,$stateParams, $ionicPopup) {
        $scope.refreshData();
        $scope.userInfo = function() {
            $state.go('user.index');
        };
        
        $scope.redPaper = function() {
            $state.go('redPaper.index');

        };
        $scope.userPage = function() {
            $state.go('user.userPage');
        };
        $scope.userInterview = function() {
            $state.go('user.userInterview');
        };
        $scope.findJob = function() {
            $state.go('user.findJob');//跳转页面名称
        };
        $scope.position = function() {//定位
            $state.go('user.position');
        };
        $scope.confirm = function() {//确定
            $state.go('user.confirm');
        };
       $scope.next = function() {//跳转下一步
            $state.go('user.next');
        };
       $scope.require = function  () {//名企招聘1
            $state.go('user.require');
        }
       $scope.requireone = function  () {//名企招聘2
            $state.go('user.require-one');
        }
       $scope.requiretwo = function  () {//名企招聘3
            $state.go('user.require-two');
        }

       $scope.nextone = function  () {
            alert("确定要跳转到下一页吗");
       }
   
       $scope.deliver = function() {//查找部分的投递简历
            $scope.publicData.popup = $ionicPopup.show({
                templateUrl: '/employ/views/deliver-popup.html',
                title: '请选择投递的简历',
                scope: $scope,
                buttons: [{
                    text: '取消'
                }, {
                    text: '<b>投递</b>',
                    type: 'button-positive',
                    onTap: function(e) {
                        return 'deliver';
                    }
                }]
            });
            $scope.publicData.popup.then(function(success) {
                if(success === 'deliver' && $scope.popup.resume) {
                    $scope.deliver($scope.popup.resume);
                }
            });
        };
    
         $scope.collect = function() {
            $http.post('/wx/employ/favorites', {
                jobId : $scope.jobId,
                userId: $scope.publicData.userInfo._id
            }).success(function(data) {
                $scope.refreshData();
            }).error(function(err) {});
        };
            
     $http.get('/wx/employ/companies').then(function(success) {//新的
            $scope.companies = success.data;
           
            return $http.get('/wx/employ/jobs');//获取后台数据
        }).then(function(success) {
            
            $scope.jobs = success.data;
            console.log(success.data);
           
        }).catch(function(err) {

        });


    })
    .controller('RedPaperIndexController', function($scope, $state) {
        $scope.home = function() {
            $state.go('user.index');
        };
        $scope.register = function() {
            $state.go('user.register');
        };
     

    })
    .controller('UserRegisterController', function($scope, $state) {
        $scope.back = function() {
            $state.go('redPaper.index');
        };
        $scope.next = function() {
            $state.go('user.registerSuccess');
        };
        $scope.getcode = function  () {//获取验证码
             alert("验证码已发到你手机上")
        }
    })
    .controller('UserRegisterSuccessController', function($scope, $state,$ionicPopup) {
        $scope.home = function() {
            $state.go('user.index');
        };
        $scope.needhelp =function () {//获取验证码帮助
            $state.go('user.needhelp');
        }
        $scope.set =function  () {//拿红包注册设置
            $state.go('user.set');
        }
        $scope.editset =function  () {//编辑拿红包注册
            $state.go('user.editset');
        }
        $scope.red =function  () {
             alert('抢红包界面弹出');//抢红包的界面
        }
     
    })
    .controller('UserPageController', function($scope, $state, $stateParams) {
        $scope.home = function() {
            $state.go('user.index');
        };
        $scope.userResume = function() {
            $state.go('user.userResume');
        };
        $scope.userWallet = function() {
            $state.go('user.userWallet');
        };
        $scope.userFavorites = function() {
            $state.go('user.userFavorites');
        };
        $scope.userInterview = function() {
            $state.go('user.userInterview');
        };
        $scope.userHistory = function() {
            $state.go('user.history');
        };
        $scope.userInfo = function() {
            $state.go('user.userInfo');
        };

    })

    .controller('UserWalletController', function($scope, $state) {
        $scope.back = function() {
            $state.go('user.userPage');
        };
    })
    .controller('UserFavoritesController', function($scope, $state) {
        $scope.goJob = function(id) {  
            $state.go('user.job', {jobId: id});
        };
       
   
        $scope.messages=[//我的收藏职位
           { name:'腾讯科技发展有限公司',position:'web前端开发工程师'},
           { name:'中国移动科技发展有限公司',position:'java开发工程师'}, 
        ]
        $scope.companyintroduce=function  () {//点击进入收藏的公司和职位
             $state.go('company.introduce');
        }

    })
    .controller('UserInterviewController', function($scope, $state) {

    })
    .controller('UserFindJobController', function($scope, $state, $http) {
        $scope.toCompany = function(id) {
            $state.go('user.company', {companyId: id});
        };
        $scope.goJob = function(id) {
            $state.go('user.job', {jobId: id});

        };
        $scope.more = function  () {//more企业页面
           
            $state.go('user.more');
        };

        $http.get('/wx/employ/companies').then(function(success) {
            $scope.companies = success.data;
            return $http.get('/wx/employ/jobs');//获取后台数据
        }).then(function(success) {
            // console.log(success.data);对应的数据的控制器
            $scope.jobs = success.data;
            // alert("获取成功")
        }).catch(function(err) {

        });
        
    })

    .controller('UserCompanyController', function($scope, $state, $http, $stateParams, $ionicLoading) {
        $ionicLoading.show({
            template: '<ion-spinner icon="android"></ion-spinner>'
        });

        $scope.back = function() {
            $state.go('user.index');
        };
        $scope.job = function(id) {
            $state.go('user.job', {jobId: id});
        };
        $scope.companyId = $stateParams.companyId;
        $http.get('/wx/employ/company', {params: {companyId: $scope.companyId}}).success(function(data) {
            $scope.company = data.company;
            $scope.jobs = data.jobs;
            $ionicLoading.hide();
        }).error(function(err) {});
    })
    .controller('UserJobController', function($scope, $state, $http, $stateParams, $ionicPopup) {
        $scope.back = function() {
            $scope.goBack();
        };


        $scope.jobId = $stateParams.jobId;
        $http.get('/wx/employ/job', {params: {jobId: $scope.jobId}})
        .then(function(success) {
            $scope.job = success.data;
            console.log(JSON.stringify($scope.job));//用json格式将数据打印出来
            return $http.post('/wx/employ/history', {
                userId: $scope.publicData.userInfo._id,
                jobId : $scope.jobId
            });
        }).catch(function(err) {
  

            $scope.goHome();
        });

        $scope.favColor = '';

        $scope.$watch('publicData.isLoading', function() {
            if($scope.publicData.isLoading) {return;}
            var isFav = $scope.publicData.favorites.filter(function(f) {
                return f._id === $stateParams.jobId;
            })[0];
            if(isFav) {$scope.favColor = 'button-assertive';}
            else {$scope.favColor = '';}
        });

        $scope.setFavorites = function() {//收藏职位
            $http.post('/wx/employ/favorites', {
                jobId : $scope.jobId,
                userId: $scope.publicData.userInfo._id
            }).success(function(data) {
                $scope.refreshData();
            }).error(function(err) {});
        };

        $scope.popup = {};
        $scope.deliver = function(resume) {
            $http.post('/wx/employ/deliver', {
                userId: $scope.publicData.userInfo._id,
                jobId : $scope.jobId,
                resumeId: resume._id
            }).success(function(data) {
                $state.go('user.userPage');//简历投递成功后的页面跳转
            });
        };

        $scope.deliverPopup = function() {
            $scope.publicData.popup = $ionicPopup.show({
                templateUrl: '/employ/views/deliver-popup.html',
                title: '请选择投递的简历',
                scope: $scope,
                buttons: [{
                    text: '取消'
                }, {
                    text: '<b>投递</b>',
                    type: 'button-positive',
                    onTap: function(e) {
                        return 'deliver';
                    }
                }]
            });
            $scope.publicData.popup.then(function(success) {
                if(success === 'deliver' && $scope.popup.resume) {
                    $scope.deliver($scope.popup.resume);
                }
            });
        };
    })
    .controller('UserHistoryController', function($scope) {
        $scope.refreshData();
    })
    .controller('DeliverSuccessController', function($scope) {
        
    })
   
;